<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

/**************库存管理********************/
class SkuController extends Controller
{
    public function index (Request $request)
    {

    	$where = []; 
    	//实例化skus表 进行多表查询goods表
    	$db = DB::table('skus');
    	$db->join('goods','skus.good_id','=','goods.id')
    	->select('skus.*','goods.title as good')
    	->get();

    	if($request->has('keywords')){
    		// 获取要搜索的name字段的值
    		$name = $request->input('keywords');
    		//添加where模糊匹配条件
    		$db->where('skus.title','like',"%{$name}%");
    		$where['title'] = $name;
    	}
		$list = $db->paginate(5);
    	return view('admin.sku.index',['list'=>$list,'where'=>$where]);
    }

    public function destroy ($id)
    {
    	$name = DB::table('skus')->where('id',$id)->delete();
    	if($name>0){
    		return redirect('admin/sku')->with('msg','修改成功');
    	}
    }
    public function edit ($id)
    {
    	$list = DB::table('skus')->first();
    	// dd($list);
    	return view('admin.sku.edit',['list'=>$list]);

    }

    public function update ($id,Request $request)
    {
    	$name = $request->except('_token','_method');
    	$list = DB::table('skus')->where('id',$id)->update($name);
    	// dd($list);
    	if($list>0){
    		return redirect('admin/sku')->with('msg','修改成功');
    	}
    }


}
